/**
 * Created by lx on 2017/5/1.
 */
layui.use(['form'], function () {
    var form = layui.form();
    var market = {};
    $(document).ready(function () {
        if (window.location.hash != null && window.location.hash != "") {
            var has = window.location.hash;
            has = has.replace("#", "");
            has = has.replace(/\'/g, "\"");
            market = JSON.parse(has);
            $('#customerId').parent().parent().hide() ;
            $('#businessId').parent().parent().hide() ;
        }
        $.get("/customer/list?pageNum=1&pageSize=1000", function (data) {
            if (data.success) {
                createOptions("customerId",data.param.list,"id","name") ;
                autoInject(market) ;
                form.render() ;
            } else {
                alert(data.error);
            }
        });
        $.get("/business/list?pageNum=1&pageSize=1000", function (data) {
            if (data.success) {
                createOptions("businessId",data.param.list,"id","name") ;
                autoInject(market) ;
                form.render() ;
            } else {
                alert(data.error);
            }
        });
        form.on('submit(yes)', function (data) {
            var  url ;
            if( market.id == undefined ){
                url = "/market/add"
            }else{
                url = "/market/update" ;
            }
            market = autoFetch(market) ;
            market.marketInfoDOs = [] ;
            if(isNotNull(market.customerId)&&isNotNull(market.businessId)){
                $.ajax({
                    type: "POST",
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(market),
                    dataType: "json",
                    success: function (data){
                        if(data.success){
                            layer.msg("增加订单成功") ;
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }else{
                            layer.msg("增加订单失败") ;
                        }
                    },
                    error: function (message) {
                        layer.msg("增加订单失败") ;
                    }
                });
            }else {
                layer.msg("请选择客户和业务") ;
            }
            return false;
        });

        $('#cancel').click(function(){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false ;
        }) ;
    });
});
